<?php

namespace App\Admin\Forms;

use App\Jobs\GenerateAgreementJob;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class UploadAgreementForm extends Form
{
    use LazyWidget;
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        $row = $this->payload['row'] ?? null;
        $agreement_file_sign = $input['agreement_file_sign'] ?? '';
        if (empty($agreement_file_sign)) {
            $this->response()->error('请上传协议文件');
        }
        // 保存协议文件
        $agreement = \App\Models\ProjectAgreement::where('id', $row['id'])->first();
        $agreement->agreement_file_sign = $agreement_file_sign;
        $agreement->save();
        return $this->response()->success('上传成功')->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->multipleImage('agreement_file_sign', '协议文件')
            ->accept('jpg,png,gif,jpeg')
            ->sortable()
            ->autoUpload()
            ->saveFullUrl()
            ->help('只允许上传jpg、png、gif、jpeg格式的图片文件')
            ->saving(function ($value) {
                return implode(',', httpToHttps($value));
            })
            ->required();
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function
    default()
    {
        return [
            'agreement_file_sign'  => $this->payload['row']['agreement_file_sign'],

        ];
    }
}
